﻿using Dapper;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using YinfangWeb.BLL.Enum;
using YinfangWeb.BLL.Model;
using YinfangWeb.Model;

namespace YinfangWeb.DAL
{
    public partial class ProductKindDAO
    {
        #region 按类获取选择中商品种类列表
        /// <summary>
		/// 按类获取选择中商品种类列表
		/// </summary>
		/// <returns>实体列表</returns>
        public List<ProductKind> GetListByType(EnumProductData_ProductType prodType, int level = -1, int state = -1)
        {
            var kl = "";//默认为全部等级
            if (level > 0)
            {
                kl = $"KindLevel={level} AND";
            }
            var ks = "";//默认为全部状态
            if (state >= 0)
            {
                ks = $"KindState={state} AND";
            }
            int type = (int)prodType;
            var sql = $"SELECT * FROM ProductKind WHERE {kl} {ks} ProductKindID like '{type}%'";
            using (var conn = ConnectionFactory.CreateConnection())
            {
                var q = conn.Query<ProductKind>(sql);
                return q.ToList();
            }
        }
        #endregion
        #region 添加单条记录
        /// <summary>
        /// 添加单条记录
        /// </summary>
        /// <param name="productKind">实体</param>
        /// <param name="trans">事务</param>
        /// <returns>影响行数</returns>
        public int Insert(ProductKind productKind)
        {
            var sql = "INSERT INTO ProductKind(ProductKindID, KindState, KindName, KindLevel, OrderNum, RegDateTime, RegUserID, RegFullName, EditDateTime, EditUserID, EditFullName) VALUES (@ProductKindID, @KindState, @KindName, @KindLevel, @OrderNum, @RegDateTime, @RegUserID, @RegFullName, @EditDateTime, @EditUserID, @EditFullName)";

            using (var conn = ConnectionFactory.CreateConnection())
            {
                conn.Open();
                conn.Query<int>(sql, productKind);
                var result = 1;
                conn.Close();
                return result;
            }
        }
        #endregion
    }
}
